package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @version v1.0
 * @package com.itheima.controller
 * @description:
 * @author: Ewaits
 * @date: 2019/7/5 10:20
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;


    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = permissionService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    @RequestMapping("add")
    public Result add(@RequestBody Permission permission) {
        try {
            permissionService.add(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增权限失败");
        }
        return new Result(true, "新增权限成功");
    }

    @RequestMapping("delete")
    public Result delete(Integer id) {
        try {
            permissionService.delete(id);
        } catch (RuntimeException re) {
            re.printStackTrace();
            return new Result(false, re.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
        return new Result(true, "删除成功");
    }

    @RequestMapping("findById")
    public Result findById(Integer id) {
        try {
            Permission permission = permissionService.findById(id);
            return new Result(true, "查询权限成功", permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询权限失败");
        }
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody Permission permission) {
        try {
            permissionService.edit(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
        return new Result(true, "修改成功");
    }

    @RequestMapping("findAllPermission")
    public Result findAll() {
        try {
            List<Permission> list = permissionService.findAll();
            if (CollectionUtils.isEmpty(list)) {
                return new Result(false, "权限暂无数据");
            }
            return new Result(true, "查询权限信息成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, "查询权限信息失败");
        }
    }

}
